{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "welsh-midnight",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[36mnumbers\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m4\u001b[39m, \u001b[32m5\u001b[39m, \u001b[32m6\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m8\u001b[39m, \u001b[32m9\u001b[39m, \u001b[32m10\u001b[39m)\r\n",
       "\u001b[36mres\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m55\u001b[39m"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n",
    "val res = numbers.foldLeft(0)((m, n) => m + n)\n",
    "print(res) // 55"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "proprietary-compression",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres1\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m55\u001b[39m"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val res1 = numbers.foldLeft(0)(_+_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "existing-pottery",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres2\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m55\u001b[39m"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val res2 = numbers.foldRight(0)(_+_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "international-fiction",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mnumbers\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m, \u001b[32m3\u001b[39m, \u001b[32m4\u001b[39m, \u001b[32m5\u001b[39m, \u001b[32m6\u001b[39m, \u001b[32m7\u001b[39m, \u001b[32m8\u001b[39m, \u001b[32m9\u001b[39m, \u001b[32m10\u001b[39m)\r\n",
       "\u001b[36mnumbersFunc\u001b[39m: (\u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m], \u001b[32mInt\u001b[39m) => \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] => \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = <function1>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val numbers = List(1,2,3,4,5,6,7,8,9,10)\n",
    "val numbersFunc = numbers.foldLeft(List[Int]())_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "foster-knife",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36msquares\u001b[39m: \u001b[32mList\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m4\u001b[39m, \u001b[32m9\u001b[39m, \u001b[32m16\u001b[39m, \u001b[32m25\u001b[39m, \u001b[32m36\u001b[39m, \u001b[32m49\u001b[39m, \u001b[32m64\u001b[39m, \u001b[32m81\u001b[39m, \u001b[32m100\u001b[39m)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val squares = numbersFunc((xs,x)=>xs:+x*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "focused-enforcement",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)\n"
     ]
    }
   ],
   "source": [
    "println(squares.toString())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "disabled-novelty",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(console):1: '=' expected but eof found.\r\n",
      "def foldLeft1[A, B](as: List[A], b0: B, op: (B, A) => B)\r\n",
      "                                                        ^"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "(console):1: '=' expected but eof found.\r\ndef foldLeft1[A, B](as: List[A], b0: B, op: (B, A) => B)\r\n                                                        ^"
     ]
    }
   ],
   "source": [
    "def foldLeft1[A, B](as: List[A], b0: B, op: (B, A) => B)\n",
    "def notPossible = foldLeft1(numbers, 0, _ + _)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "judicial-section",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "cmd8.sc:1: not found: value foldLeft1\n",
      "def firstWay = foldLeft1[Int, Int](numbers, 0, _ + _)\n",
      "               ^Compilation Failed"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Compilation Failed"
     ]
    }
   ],
   "source": [
    "def firstWay = foldLeft1[Int, Int](numbers, 0, _ + _)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "minute-reproduction",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "cmd8.sc:1: not found: value foldLeft1\n",
      "def secondWay = foldLeft1(numbers, 0, (a: Int, b: Int) => a + b)\n",
      "                ^Compilation Failed"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Compilation Failed"
     ]
    }
   ],
   "source": [
    "def secondWay = foldLeft1(numbers, 0, (a: Int, b: Int) => a + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "moral-abortion",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
